unit MainUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, Data.DB, cxDBData, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  FireDAC.Stan.StorageBin, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, Vcl.StdCtrls, cxGridLevel, cxClasses, cxGridCustomView, cxGrid;

type
  TmainForm = class(TForm)
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Button1: TButton;
    FDMemTable1: TFDMemTable;
    FDMemTable1id: TStringField;
    FDMemTable1memo: TStringField;
    DataSource1: TDataSource;
    cxGrid1DBTableView1id: TcxGridDBColumn;
    cxGrid1DBTableView1memo: TcxGridDBColumn;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure cxGrid1DBTableView1SelectionChanged(Sender: TcxCustomGridTableView);
  public
    { Public declarations }
  end;

var
  mainForm: TmainForm;

implementation

{$R *.dfm}

uses ChildUnit;

procedure TmainForm.Button1Click(Sender: TObject);
begin
  childForm.cxGrid1DBTableView1.OnSelectionChanged:=cxGrid1DBTableView1SelectionChanged;
  childForm.Show;
end;

procedure TmainForm.cxGrid1DBTableView1SelectionChanged(Sender: TcxCustomGridTableView);
var
  i:integer;
  str,str1:string;
begin

  for i:=0 to Sender.ViewData.RecordCount-1 do
  begin
    if sender.ViewData.Records[i].Selected then
    begin
      if  FDMemTable1.Lookup('id',Sender.ViewData.Records[i].Values[sender.FindItemByName('cxGrid1DBTableView1id').Index],'id') = null then
      begin
        FDMemTable1.Append;
        FDMemTable1.FieldValues['id']:= Sender.ViewData.Records[i].Values[sender.FindItemByName('cxGrid1DBTableView1id').Index];
        FDMemTable1.FieldValues['memo']:= Sender.ViewData.Records[i].Values[sender.FindItemByName('cxGrid1DBTableView1memo').Index];
        FDMemTable1.Post;
      end;
    end
    else
    begin
      if FDMemTable1.Locate('id',Sender.ViewData.Records[i].Values[sender.FindItemByName('cxGrid1DBTableView1id').Index]) then
      begin
        FDMemTable1.Delete;
      end;

    end;
  end;
end;

end.
